//JS
var app = getApp()
let orderStatus = 0; //-1订单取消,0新下单待上餐,1待用户评价,2订单已完成
let db = wx.cloud.database();
Page({

    /**
     * 页面的初始数据
     */
    data: {
    // 顶部菜单切换
    navbar: ["待上餐", "待评价", "已完成", "已取消"],
    // 默认选中菜单
    currentTab: 0,
    list:[]
    },
      //顶部tab切换
  navbarTap: function (e) {
    let index = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: index
    })
    if (index == 0) {
      orderStatus = 0;
    } else if (index == 1) {
      orderStatus = 1;
    } else if (index == 2) {
      orderStatus = 2;
    } else if (index == 3) {
      orderStatus = -1;
    } else {
      orderStatus = 0;
    }
    this.getMyOrderList(orderStatus);
  },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
     this.getMyOrderList(0);
    },
    getMyOrderList() {
       db.collection("order").
       where({
           status: orderStatus
       }).orderBy('_createTime','desc').get()
       .then(res=>{
         this.setData({
          list: res.data
         })
         }).catch(err=>{
         console.log(err);
       })
        
      },

      //取消订单
      cancelOrder(e){
       let id = e.currentTarget.dataset.id;

        db.collection("order").doc(id).
       update({
         data:{
           status: -1
         }
       }).then(res=>{
         this.getMyOrderList(0);
       }).catch(err=>{
         console.log(err);
       })
      },

      //评价
      pingjia(e){
        let id = e.currentTarget.dataset.id;

        wx:wx.showModal({
       
          cancelText: '取消',
       
          confirmText: '提交',
          editable: true,
      showCancel: true,
          title: '评价',
          success: (result) => {
            console.log(result)
            if(result.confirm){
                  //点击提交
                  let user = wx.getStorageSync('user');
                  console.log(user)
                  db.collection("pinglun").add({
                    data:{
                      name: user.nickName,
                      orderId: id,
                      content: result.content,
                      avatarUrl: user.avatarUrl,
                      _createTime: app.getCurrentTime()
                    }
                  }).then(res=>{
                    db.collection("order").doc(id).update({
                      data: {
                        status: 2
                      }
                    }).then(res=>{
                      this.getMyOrderList(1);
                      wx.showToast({
                        title: '评价成功',
                      })
                      
                    }).catch(err=>{
                      console.log("更新订单状态为已完失败",err)
                    })
                 
                  })
            }
          },
          fail: (res) => {
            console.log(err);
          },
      
        })
      },

      chakanpingjia(){
        wx.navigateTo({
          url: '/pages/mycomment/mycomment'
        })
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})